package com.samskievert.cactusboom;

import com.threerings.media.sprite.ImageSprite;
import java.awt.Graphics2D;

import java.awt.Color;
import java.awt.geom.AffineTransform;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.Rectangle;
import com.threerings.media.util.LinePath;
import java.util.Iterator;

import com.threerings.media.image.BufferedMirage;
import com.threerings.media.image.Mirage;


public class CowboySprite extends ImageSprite
{
    public static final int SIZE = 48; 
    public double scale = 1.0;
    private CBObject.Cowboy _cowboy;
    private Dimension _boardsize;
    private BufferedMirage _image;
    private CBLogic.movePath _movepath;
    
    public CowboySprite(CBObject.Cowboy cowboy, Dimension boardsize, BufferedMirage image)
    {
        super(image);
        setLocation(cowboy.x*SIZE, cowboy.y*SIZE);
        _cowboy = cowboy;
        _boardsize = boardsize;
        _image = image;
        setRenderOrder(10);
    }
    
    public CBObject.Cowboy getCowboy()
    {
        return _cowboy;
    }
    
    public void updateCowboy(CBObject.Cowboy cowboy)
    {
     //System.out.println("updateCowboy command received!");
     if(cowboy.x != _cowboy.x || cowboy.y != _cowboy.y)
     {
        //figure out how many moves away this is
        int tD = Math.abs(cowboy.x - _cowboy.x) + Math.abs(cowboy.y - _cowboy.y);
        //move there at a rate of one block/500 ms
        move(new LinePath(new Point(cowboy.x*SIZE, cowboy.y*SIZE), 500*tD));
     }
     _cowboy = cowboy;
     invalidate();
    }
    
    @Override // from Sprite
    public void paint (Graphics2D gfx)
    {
        
        Color[] COLORLIST = new Color[] {Color.blue, Color.red, Color.green, Color.orange};
        
        int[] hx = {3,3,18,18,24,30,30,45,45};
        int[] hy = {16,12,12,6,7,6,12,12,16};
        Polygon hat = new Polygon(hx,hy,9);
        
        int[] hx2 = {16,18,22,26,30,32};
        int[] hy2 = {16,20,24,24,20,16};
        Polygon head = new Polygon(hx2,hy2,6);
        
        AffineTransform currentRot = gfx.getTransform();
        gfx.scale(scale, scale);
        
        super.paint(gfx);
        
        /*
        gfx.setColor(COLORLIST[_cowboy.owner]);
        gfx.fill(hat);
        gfx.fillRect(12,26,24,16); //the body
        Color flesh = new Color(196,196,96);
        gfx.setColor(flesh);
        gfx.fill(head);
        gfx.fillRect(23,24,2,2);   //the neck
        //now draw the black outlines
        gfx.setColor(Color.black);
        gfx.draw(hat);
        gfx.draw(head);
        gfx.drawRect(23,24,2,2);
        gfx.drawRect(12,26,24,16);
       */
        gfx.setTransform(currentRot);
    }
}

